<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Api\Service\Qrcode\Controllers;

use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\ProductFields;
use App\Eloquent\Ygt\Qrcode;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class BindingController extends Controller
{
    public $userId          = 25;
    //绑定材料
    public function product()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $qrcodeStr          = request('qrcode','');
        $productId          = request('product_id',0);
        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER,'登录过期' );
        }
        //一些必填字段
        $requestMustArr     = [
            'qrcode_id'=>'二维码id',
            'product_id'=>'材料id',
            'stock_id'=>'入库流水id',
        ];
        $requestData        = request()->all();
        foreach($requestMustArr as $key=>$val)
        {
            if(!isset($requestData[$key]) || empty($requestData[$key]))
            {
                $requestMustMsg     = $val;
                xThrow( ERR_PARAMETER,$requestMustMsg );
            }
        }
        //根据不同角色取不同表和模型
        $tableArr           = Func::getQrocdeTableArr($userInfo);
        $qrcodeModel        = $tableArr['qrcode_model'];
        $qrcodeFieldsModel  = $tableArr['fields_model'];
        $qrcodeLogModel     = $tableArr['log_model'];

        //解析二维字符串
        $qrcodeArr          = $qrcodeModel::decodeStr($qrcodeStr,false);
        if($qrcodeArr==false)
        {
            xThrow(ERR_UNKNOWN,'二维码解析出错');
        }
        $qrcodeId           = $qrcodeArr['id'];
        $where              = ['id'=>$qrcodeId];
        $qrcode             = $qrcodeModel::getInfo($where);
        if(!$qrcode)
        {
            xThrow(ERR_UNKNOWN,'二维码信息出错');
        }
        $qrcodeType         = $qrcodeModel::getQrcodeType($qrcode);
        if($qrcodeType!=3)
        {
            xThrow(ERR_UNKNOWN,'二维码已经被使用了');
        }
        $where              = ['id'=>$qrcodeId];
        $data               = ['product_id'=>$productId];
        $updateResult       = $qrcodeModel::updateOneData($where,$data);
        if($updateResult === false)
        {
            xThrow(ERR_UNKNOWN,'二维码绑定失败');
        }
        $result             = [
            'message'=>'绑定成功'
        ];
        return $result;
    }
    //二维码绑定物品信息
    public function confirm()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $qrcodeStr          = request('qrcode','');
        $productId          = request('product_id',0);
        $number             = request('number',0);
        $unit               = request('unit','');
        $place_name         = request('place_name','');
        $requestNonArr      = request('non_arr');
        $nonArr             = json_decode(htmlspecialchars_decode($requestNonArr), true);
        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER,'登录过期' );
        }
        //一些必填字段
        $requestMustArr     = [
            //'qrcode_id'=>'二维码id',
            'product_id'=>'材料id',
            //'stock_id'=>'入库流水id',
            'number'=>'数量',
        ];
        $requestData        = request()->all();
        foreach($requestMustArr as $key=>$val)
        {
            if(!isset($requestData[$key]) || empty($requestData[$key]))
            {
                $requestMustMsg     = $val;
                xThrow( ERR_PARAMETER,$requestMustMsg );
            }
        }
        //根据不同角色取不同表和模型
        $tableArr           = Func::getQrocdeTableArr($userInfo);
        $qrcodeModel        = $tableArr['qrcode_model'];
        $qrcodeFieldsModel  = $tableArr['fields_model'];
        $qrcodeLogModel     = $tableArr['log_model'];

        //解析二维字符串
        $qrcodeArr          = $qrcodeModel::decodeStr($qrcodeStr,false);
        if($qrcodeArr==false)
        {
            xThrow(ERR_UNKNOWN,'二维码解析出错');
        }
        $qrcodeId           = $qrcodeArr['id'];
        $where              = ['id'=>$qrcodeId];
        $qrcode             = $qrcodeModel::getInfo($where);
        if(!$qrcode)
        {
            xThrow(ERR_UNKNOWN,'二维码信息出错');
        }
        $qrcodeType         = $qrcodeModel::getQrcodeType($qrcode);
        if($qrcodeType!=3)
        {
            xThrow(ERR_UNKNOWN,'二维码已经被使用了');
        }

        //绑定入库
        $where                  = ['type'=>2,'product_id'=>$productId];
        $nonstandardArr         = ProductFields::getList($where)->toArray();
        $fieldsInsertResult     = $nonFieldsResult = true;
        $fieldsDataAll          = $nonFieldsResultArr = [];
        if($nonArr)
        {
            foreach($nonArr as $key=>$val)
            {
                $valFieldName           = $val['field_name'];
                $valFieldValue          = $val['field_value'];
                $valFieldUnit           = isset($val['unit']) ? $val['unit'] : '';
                $valFieldValue          = intval($valFieldValue);
                //检测非标属性是否必填或是是在指定范围之内
                foreach($nonstandardArr as $k=>$v)
                {
                    $vIsMust            = $v['is_must'];
                    $vFieldName         = $v['field_name'];
                    $vMinValue          = $v['min_value'];
                    $vMaxValue          = $v['max_value'];
                    if ($valFieldName == $vFieldName)
                    {
                        if ($valFieldValue < $vMinValue || $valFieldValue > $vMaxValue) {
                            $nonFieldsResult = false;
                            $nonFieldsResultArr[] = $valFieldName . '不在范围内';
                        }
                        if ($vIsMust == 1 && !$valFieldValue) {
                            $nonFieldsResult = false;
                            $nonFieldsResultArr[] = $valFieldName . '必填';
                        }
                    }
                }
                $temp                   = [
                    'qrcode_id'=>$qrcodeId,
                    'field_name'=>$valFieldName,
                    'field_value'=>$valFieldValue,
                    'field_unit'=>$valFieldUnit,
                ];
                $fieldsDataAll[]        = $temp;
            }
            if($nonFieldsResult === false)
            {
                $nonFieldsResultMessage     = implode(',',$nonFieldsResultArr);
                xThrow(ERR_PARAMETER, $nonFieldsResultMessage);
            }
            if(!empty($fieldsDataAll))
            {
                $fieldsInsertResult     = \DB::table('ygt_qrcode_fields')->insert($fieldsDataAll);
            }
        }
        if($fieldsInsertResult === false)
        {
            xThrow(ERR_UNKNOWN,'绑定物品信息失败');
        }
        //更新二维码的数量
        $qrcodeData             = [
            'product_id'=>$productId,
            'total_number'=>$number,
            'now_number'=>$number,
            'unit'=>$unit,
            'place_name'=>$place_name,
        ];
        $where                  = ['id'=>$qrcodeId];
        $updateResult           = $qrcodeModel::updateOneData($where,$qrcodeData);
        if($updateResult === false)
        {
            xThrow(ERR_UNKNOWN,'二维码信息更新失败');
        }
        $result             = [
            'message'=>'绑定成功',
            'qrcode_id'=>$qrcodeId,
        ];
        return $result;
    }
}